---
category: Misc
created: '2020-06-12'
title: Get the first defined and non null argument
updated: '2021-10-13'
---

**JavaScript version**

```js coalesce.js
const coalesce = (...args) => args.find((item) => item !== undefined && item !== null);

// Or
const coalesce = (...args) => args.find((item) => ![undefined, null].includes(item));
```

**TypeScript version**

```ts coalesce.ts
const coalesce = (...args: any[]): any[] => args.find((item) => item !== undefined && item !== null);

// Or
const coalesce = (...args: any[]): any[] => args.find((item) => ![undefined, null].includes(item));
```

**Examples**

```js examples.js
coalesce(undefined, null, 'helloworld', NaN); // 'helloworld'
```
